package com.google.code.irouter.batch.api;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Transaction;
import com.google.appengine.api.taskqueue.QueueFactory;
import com.google.appengine.api.taskqueue.TaskOptions;
import com.google.code.irouter.model.Friend;
import com.google.code.irouter.model.Human;
import com.google.code.irouter.model.Model;
import com.google.code.irouter.model.Product;
import com.google.code.irouter.plugin.salesman.Sina;
import com.google.code.irouter.utils.DateTimeUtil;
import com.google.code.irouter.utils.ModelUtil;

public final class Salesman implements Serializable {

	private static final long serialVersionUID = 2267291343869925546L;
	
	private static long HOUR = 1L * 60L * 60L * 1000L;
	
	private static Date timeSeq;
	
	public static void execute() {
		if(timeSeq == null)
			timeSeq = DateTimeUtil.getNow();
		Map<String, Object> query = new HashMap<String, Object>();
		query.put("timeSeq", timeSeq);
		List<Model> modelList = ModelUtil.selectNot(Product.class, query);
		Product product = null;
		if(modelList.size() == 0) {
			timeSeq = DateTimeUtil.getNow();
			modelList = ModelUtil.select(Product.class);
			if(modelList.size() > 0) {
				product = (Product) modelList.get(0);
			}
		} else {
			product = (Product) modelList.get(0);
		}
		if(product == null) {
			end();
			return;
		}
		
		String type = product.getType();
		String country = product.getCountry();
		String province = product.getProvince();
		String city = product.getCity();
		query = new HashMap<String, Object>();
		query.put("type", type);
		if(country != null)
			if(country.trim().length() > 0)
				query.put("country", country);
		if(province != null)
			if(province.trim().length() > 0)
				query.put("province", province);
		if(city != null)
			if(city.trim().length() > 0)
				query.put("city", city);
		modelList = ModelUtil.select(Human.class, query);
		if(modelList.size() == 0) {
			end();
			return;
		}
		Human human = (Human) modelList.get(0);
		query = new HashMap<String, Object>();
		query.put("id", human.getId());
		if(ModelUtil.select(Friend.class, query).size() > 0) {
			ModelUtil.remove(human);
			end();
			return;
		}
		product.setTimeSeq(timeSeq);
		ModelUtil.save(product);
		
		if(product.getCountFollow() == null) {
			product.setCountFollow(1);
		}
		if(product.getCountFollow() >= product.getQuotaFollow()) {
			if(DateTimeUtil.getDateInterval(DateTimeUtil.getNow(), product.getLastFollow()) >= 1 * HOUR) {
				product.setCountFollow(1);
			}
		}

		if(product.getCountFollow() == 1) {
			product.setLastFollow(DateTimeUtil.getNow());
		}
		
		if(product.getCountFollow() <= product.getQuotaFollow()) {
			if(product.getType().equals("SinaWeiboBot")) {
				Sina sina = new Sina();
				if(sina.follow(product, human)) {
					Friend friend = new Friend();
					friend.setType(human.getType());
					friend.setId(human.getId());
					friend.setName(human.getName());
					friend.setGender(human.getGender());
					friend.setCountry(human.getCountry());
					friend.setProvince(human.getProvince());
					friend.setCity(human.getCity());
					friend.pushCreatDate();
					ModelUtil.save(friend);
					ModelUtil.remove(human);
				}
			}
			product.setCountFollow(product.getCountFollow() + 1);
			ModelUtil.save(product);
			end();
			return;
		}
		
		product = (Product) ModelUtil.select(Product.class, product.getKey()).get(0);
		
		if(product.getCountComment() == null) {
			product.setCountComment(1);
		}
		if(product.getCountComment() >= product.getQuotaComment()) {
			if(DateTimeUtil.getDateInterval(DateTimeUtil.getNow(), product.getLastComment()) >= 1 * HOUR) {
				product.setCountComment(1);
			}
		}

		if(product.getCountComment() == 1) {
			product.setLastComment(DateTimeUtil.getNow());
		}
		
		if(product.getCountComment() <= product.getQuotaComment()) {
			if(product.getType().equals("SinaWeiboBot")) {
				Sina sina = new Sina();
				if(sina.comment(product, human)) {
					Friend friend = new Friend();
					friend.setType(human.getType());
					friend.setId(human.getId());
					friend.setName(human.getName());
					friend.setGender(human.getGender());
					friend.setCountry(human.getCountry());
					friend.setProvince(human.getProvince());
					friend.setCity(human.getCity());
					friend.pushCreatDate();
					ModelUtil.save(friend);
					ModelUtil.remove(human);
				}
			}
			product.setCountComment(product.getCountComment() + 1);
			ModelUtil.save(product);
		}
		end();

	}
	
	private static void end() {
		DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
		com.google.appengine.api.taskqueue.Queue queue = QueueFactory
				.getQueue("sale");
		Transaction txn = ds.beginTransaction();
		queue.add(TaskOptions.Builder.withUrl("/cron/sale")
				.countdownMillis(1000 * 30)
				.method(TaskOptions.Method.GET));
		txn.commit();
	}

}
